﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Active.Controls
{
    /// <summary>
    /// Menu 的摘要说明
    /// </summary>
    public class Menu : Control
    {
        public Menu()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        #region Properties

        private bool _StartFromCurrentNode = false;
        /// <summary>
        /// 设置是否优先显示子节点, 当当前节点没有子节点时, 显示当前所在父节点的子节点
        /// </summary>
        public bool StartFromCurrentNode
        {
            get { return _StartFromCurrentNode; }
            set { _StartFromCurrentNode = value; }
        }


        private string _Css = string.Empty;
        /// <summary>
        /// 设置Css样式
        /// </summary>
        public string Css
        {
            get
            {
                return _Css;
            }
            set
            {
                _Css = value;
            }
        }
        
        private static object _SyncRoot = new object();
        private static string _Html;
        /// <summary>
        /// Caches the rendered HTML in the private field.
        /// </summary>
        private string Html
        {
            get
            {
                //if (_Html == null)
                //{
                //    lock (_SyncRoot)
                //    {
                //        if (_Html == null)
                //        {

                //if (!Page.User.Identity.IsAuthenticated)
                //    return "";
                            HtmlGenericControl div = BingLinks();
                            
                            if (Css.Length > 0)
                            {
                                div.Attributes.Add("class", Css);
                            }
                            System.IO.StringWriter sw = new System.IO.StringWriter();
                            div.RenderControl(new HtmlTextWriter(sw));
                            _Html = sw.ToString();
                //        }
                //    }
                //}
                return _Html;
            }
        }

        private HtmlGenericControl BingLinks()
        {
            
            HtmlGenericControl div = new HtmlGenericControl("div");
            if (!SiteMap.RootNode.HasChildNodes)
            {
                return div;
            }
            
            SiteMapNode node = SiteMap.RootNode.ChildNodes[0];
            while (node != null)
            {
                if (node["visible"] == "false" || !node.IsAccessibleToUser(HttpContext.Current))
                {
                    node = node.NextSibling;
                    continue;
                }
                

                HtmlGenericControl ul1 = new HtmlGenericControl("ul");
                HtmlGenericControl li1 = new HtmlGenericControl("li");
                if (node.HasChildNodes)
                {
                    Literal anc1 = new Literal();
                    anc1.Text = string.Format(
                                @"<a href='{0}' title='{1}'>{2}<!--[if IE 7]><!--></a><!--<![endif]-->
                                <!--[if lte IE 6]><table><tr><td><![endif]-->", 
                                  //string.IsNullOrEmpty(node.Url) ? node.ChildNodes[0].Url : node.Url, 
                                  string.IsNullOrEmpty(node.Url) ? VirtualPathUtility.ToAbsolute(node["path"]) : node.Url, 
                                  node.Description, 
                                  node.Title);

                    li1.Controls.Add(anc1);

                    HtmlGenericControl ul2 = new HtmlGenericControl("ul");
                    foreach (SiteMapNode childNode in node.ChildNodes)
                    {
                        if (childNode["visible"] == "false")
                            continue;

                        //隐藏不直接显示的页面链接
                        bool isBreak = childNode.Roles.Count != 0;
                        foreach (object role in childNode.Roles)
                        {
                            if (Csla.ApplicationContext.User.IsInRole(role.ToString()))
                            {
                                isBreak = false;
                                break;
                            }
                        }
                        if (isBreak)
                        {
                            continue;
                        }

                        HtmlGenericControl li2 = new HtmlGenericControl("li");
                        HtmlAnchor anc2 = new HtmlAnchor();
                        anc2.HRef = childNode.Url;
                        anc2.InnerHtml = childNode.Title;
                        anc2.Title = childNode.Description;
                        li2.Controls.Add(anc2);
                        ul2.Controls.Add(li2);
                    }
                    li1.Controls.Add(ul2);
                    Literal html = new Literal();
                    html.Text = "<!--[if lte IE 6]></td></tr></table></a><![endif]-->";
                    li1.Controls.Add(html);
                }
                else
                {
                    HtmlAnchor anc1 = new HtmlAnchor();
                    anc1.HRef = node.Url;
                    anc1.InnerHtml = node.Title;
                    anc1.Title = node.Description;
                    li1.Controls.Add(anc1);
                }
                ul1.Controls.Add(li1);
                div.Controls.Add(ul1);
                node = node.NextSibling;
            }
            return div;
        }

        #endregion

        /// <summary>
        /// Renders the control.
        /// </summary>
        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.Write(Html);
            writer.Write(Environment.NewLine);
        }
    }
}
